package com.example.androidcoursedesing.login;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.example.androidcoursedesing.MainActivityExample;
import com.example.androidcoursedesing.R;
import com.example.androidcoursedesing.main.MainActivity;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class LoginAct extends AppCompatActivity implements View.OnClickListener {
    private EditText input_username;
    private EditText input_password;
    private Button login_bt;
    private TextView register_tv;
    private OkHttpClient client ;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getSupportActionBar().hide();
        input_username = (EditText) findViewById(R.id.et_loginactivity_username);
        input_password = (EditText) findViewById(R.id.et_loginactivity_password);
        login_bt = findViewById(R.id.bt_loginactivity_login);
        register_tv=findViewById(R.id.tv_loginactivity_register);
        //登录
        login_bt.setOnClickListener(this);
        //注册
        register_tv.setOnClickListener(this);
        //初始化okhttpclient
        client = new OkHttpClient();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_loginactivity_login:
                login();
                break;
            case  R.id.tv_loginactivity_register:
                register();
                break;
        }

    }


    private void login() {
        String username = input_username.getText().toString();
        String password = input_password.getText().toString();

        UserBean userBean = new UserBean();
        userBean.setUsername(username);
        userBean.setPassword(password);
        //把需要上传的实体类转成json数据方便与后台交互
        // 创建Gson实例
        Gson gson = new Gson();
        // 由目标对象转成json数据：调用Gson的toJson方法
        String userDataForJson = gson.toJson(userBean);

         //构造 RequestBody 需要指定MediaType，用于描述请求/响应 body 的内容类型
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        //登录请求接口url
        String url="http://10.111.34.2:8089/userLogin";
        //发起登录的网络请求
        Request loginRequest = new Request.Builder()
                .post(RequestBody.create(userDataForJson, mediaType))
                .url(url)
                .build();
        //发起请求
        client.newCall(loginRequest).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String responseData = response.body().string();
                Map data = gson.fromJson(responseData, HashMap.class);
                //获取登录状态码和返回消息
                String code = (String) data.get("code");
                String msg = (String) data.get("msg");
                if ("000".equals(code)) {
                    Intent intent = new Intent(LoginAct.this, MainActivity.class);
                    startActivity(intent);
                    LoginAct.this.finish();
                }else {
                    //涉及到UI线程刷新不能在子线程中执行
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(LoginAct.this,  msg, Toast.LENGTH_SHORT).show();
                        }
                    });

                }
            }
        });


    }


    private void register() {
        Intent intent = new Intent(LoginAct.this, RegisterAct.class);
        startActivity(intent);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

    }
}
